package com.ch.home.controller.userController;

import com.ch.home.bean.Commodity;
import com.ch.home.bean.CommodityRecord;
import com.ch.home.service.serviceImpl.mallServiceImpl.MallServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;

/**
 * Created by CHEN on 2019/8/25.
 */

@RestController
@RequestMapping("/user/mall")
public class MallController {


    @Autowired
    MallServiceImpl mallService;

    /**
     * 添加商品信息
     *
     * @return
     */
    @PostMapping("/addCommodity")
    public HashMap addCommodity(
            @RequestParam(required = false) String commodityName,
            @RequestParam(required = false) Integer commodityPrice,
            @RequestParam(required = false) Integer commodityDiscount,
            @RequestParam(required = false) String commodityDescription,
            @RequestParam(required = false) Integer commodityRepertory,
            @RequestParam(required = false) String commodityUrl,
            @RequestParam(required = false) Integer status) {

        HashMap<Object, Object> result;

        Commodity commodity = new Commodity(null, commodityName, commodityPrice, commodityDiscount, commodityDescription, commodityRepertory, commodityUrl, new Date( ), status);
        result = mallService.addCommodity(commodity);
        return result;
    }


    /**
     * 修改商品信息
     *
     * @return
     */
    @PostMapping("/updateCommodity")
    public HashMap updateCommodity(
            @RequestParam(required = false) Integer commodityId,
            @RequestParam(required = false) String commodityName,
            @RequestParam(required = false) Integer commodityPrice,
            @RequestParam(required = false) Integer commodityDiscount,
            @RequestParam(required = false) String commodityDescription,
            @RequestParam(required = false) Integer commodityRepertory,
            @RequestParam(required = false) String commodityUrl,
            @RequestParam(required = false) Integer status) {
        HashMap<Object, Object> result;
        Commodity commodity = new Commodity(commodityId, commodityName, commodityPrice, commodityDiscount, commodityDescription, commodityRepertory, commodityUrl, new Date( ), status);
        result = mallService.updateCommodity(commodity);
        return result;
    }

    /**
     * 拿到商品简略信息，
     *
     * @return
     */
    @PostMapping("/getPreview")
    public HashMap getPreview(
            @RequestParam(required = false) Integer commodityId,
            @RequestParam(required = false) String commodityName,
            @RequestParam(required = false) String commodityDescription
    ) {

        HashMap<Object, Object> result;
        Commodity commodity = new Commodity(commodityId, commodityName, null, null, commodityDescription, null, null, new Date( ), null);
        result = mallService.getCommodityPreview(commodity);
        return result;
    }

    /**
     * 拿到商品全部信息
     *
     * @return
     */
    @PostMapping("/getAll")
    public HashMap getAllInfo(
            @RequestParam(required = false) Integer commodityId,
            @RequestParam(required = false) String commodityName,
            @RequestParam(required = false) String commodityDescription) {
        HashMap<Object, Object> result;
        Commodity commodity = new Commodity(commodityId, commodityName, null, null, commodityDescription, null, null, new Date( ), null);
        result = mallService.getCommodityAll(commodity);
        return result;
    }

    /**
     * 生成订单
     *
     * @return
     */
    @PostMapping("/generateOrder")
    public HashMap getOrder(
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) Integer commodityId,
            @RequestParam(required = false) Integer num) {

        HashMap<Object, Object> result;

        result = mallService.generateOrder(userId, commodityId, num);

        return result;
    }

    /**
     * 购买，写入数据库
     *
     * @return
     */
    @PostMapping("/buy")
    public HashMap buy(
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) Integer commodityId,
            @RequestParam(required = false) Integer sum,
            @RequestParam(required = false) Integer num) {
        HashMap<Object, Object> result;

        CommodityRecord commodityRecord = new CommodityRecord(null, userId, commodityId, new Date( ), sum, num);
        result = mallService.buy(commodityRecord);
        return result;
    }

    /**
     * 消费记录查询
     *
     * @return
     */
    @PostMapping("/getRecord")
    public HashMap getRecord(
            @RequestParam(required = false) Integer id,
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) Integer commodityId) {

        HashMap<Object, Object> result;
        CommodityRecord commodityRecord = new CommodityRecord(id, userId, commodityId, null, null, null);

        result = mallService.getRecord(commodityRecord);

        return result;
    }


}
